package com.fsq.xmfsubdomain;

import java.net.InetAddress;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;

import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

//此类目前只实现了爱站反查
public class ReverseLookupThread extends Task<String> {

	private CountDownLatch countDownLatch;
	private LinkedBlockingQueue<String> queue;
	private Set<InetAddress> blacklist;
	private TreeView<String> id_treeview;
	private TableView<Row> id_tableview;
	private String domain;
	private Label id_status;
	private Set<String> ipList;
	private Set<Record> allRecordSet;
	private TreeItem<String> tiLevel2;

	public ReverseLookupThread(CountDownLatch countDownLatch, LinkedBlockingQueue<String> queue,
							   Set<InetAddress> blacklist, TreeView<String> id_treeview, TableView<Row> id_tableview, String domain,
							   Label id_status, Set<String> ipList, Set<Record> allRecordSet, TreeItem<String> tiLevel2) {
		super();
		this.countDownLatch = countDownLatch;
		this.queue = queue;
		this.blacklist = blacklist;
		this.id_treeview = id_treeview;
		this.id_tableview = id_tableview;
		this.domain = domain;
		this.id_status = id_status;
		this.ipList = ipList;
		this.allRecordSet = allRecordSet;
		this.tiLevel2 = tiLevel2;
	}

	@Override
	protected String call() throws Exception {

		try {
			ObservableList<Row> items = id_tableview.getItems();
			String ip;
			while ((ip = queue.poll(3, TimeUnit.SECONDS)) != null) {
				// 爱站反查
				// Mozilla/5.0 (Windows NT 6.3; WOW64; rv:46.0) Gecko/20100101
				// Firefox/46.0
				if (blacklist.contains(ip)) {
					continue;
				}
				String ip2 = ip;
				Platform.runLater(new Runnable() {

					@Override
					public void run() {
						id_status.setText("正在使用爱站反查ip：" + ip2);

					}
				});

				String userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0";
				CloseableHttpClient httpclient = HttpClients.custom().setUserAgent(userAgent).build();
				RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000)
						.setConnectionRequestTimeout(3000).setSocketTimeout(3000).build();
				for (int i = 1; i <= 10; i++) {
					String url = "http://dns.aizhan.com/" + ip + "/" + i + "/";
					HttpGet httpGet = new HttpGet(url);
					httpGet.setConfig(requestConfig);
					httpGet.addHeader("X-FORWARDED-FOR", ip);
					httpGet.addHeader("Referer", url);
					System.out.println("当前url:" + url);
					CloseableHttpResponse response = httpclient.execute(httpGet);
					HttpEntity entity = response.getEntity();
					String pagedoc = EntityUtils.toString(entity, "utf-8");
					// System.out.println(pagedoc);
					Document doc = Jsoup.parse(pagedoc);
					Elements aElements = doc.getElementsByAttributeValue("rel", "nofollow");

					if (aElements.size() <= 7) {
						if (pagedoc.contains("您的查询太频繁了") || pagedoc.contains("已超过限制次数")) {
							System.out.println("由于查询太频繁，已被爱站封锁");
							Platform.runLater(new Runnable() {

								@Override
								public void run() {
									id_status.setText("由于查询次数太多已被爱站封锁,直接进入下一步");
								}
							});
							queue.clear();
							countDownLatch.countDown();
							return null;
						}
						Thread.sleep(1000);
						break;
					}
					String domainname = null;
					for (Element element : aElements) {
						domainname = element.text();
						// System.out.println(domainname);
						// 如果不是目标域名的子域名则下一个
						if (!domainname.contains(domain)) {
							continue;
						}
						System.out.println(domain + "来自爱站的域名: " + domainname);

						Record[] records = new Lookup(domainname).run();
						if (records != null) {
							for (Record record : records) {

								if (allRecordSet.add(record)) {
									// 添加成功说明之前没有收录
									Row row = new Row(domain, domainname, ip, "爱站", "2", "A记录");
									TreeItem<String> tiAizhan = new TreeItem<String>(domainname);
									Platform.runLater(new Runnable() {

										@Override
										public void run() {
											tiLevel2.getChildren().add(tiAizhan);
											items.add(row);
										}
									});

								} else {
									String d = domainname;
									Platform.runLater(new Runnable() {

										@Override
										public void run() {
											id_status.setText("收到来自爱站的域名为重复记录:" + d);
										}
									});
								}
							}

						}
						Thread.sleep(1000);
					}

				}
			}
		}

		catch (Exception e) {
			e.printStackTrace();
		} finally {
			countDownLatch.countDown();
			System.out.println("反查线程退出。");
		}
		return null;
	}

}
